<?php

/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2020/2/5
 * Time: 22:05
 */

namespace app\api\controller;


use app\api\Base2Controller;
use think\Session;
use think\Db;

class Fuuser extends Base2Controller
{

    /**
     * 修改密码
     */
    public function updatepassword()
    {
        if (!(input('password')) || !(input('newpassword')) || !(input('newtwopassword'))) {
            return  json_fail_return('请正确输入信息');
        }
        if (input('newpassword') != input('newtwopassword')) {
            return json_fail_return('两次密码输入不一致');
        }
        $personalmodel = model('fupersonal');
        $condition = [
            'id' => $this->user_id,
            'password' => md5(input('password'))
        ];
        $id = $personalmodel->info($condition, 'id');
        if (empty($id)) {
            return json_fail_return('旧密码错误');
        }
        $bool = $personalmodel->edit(['id' => $id['id']], ['password' => md5(input('newpassword'))]);
        if ($bool !== false) {
            return json_success_return([], '修改密码成功');
        } else {
            return json_fail_return('修改密码失败');
        }
    }

    /**
     * 我的邀请码
     */
    public function mycode()
    {
        $userid = $this->user_id;
        $model = model('fupersonal');
        $mycode = $model->info(['id' => $userid], 'code,fuwu_code,fuwu_img_code');
        return json_success_return(['code' => $mycode['code'], 'img_code' => $mycode['fuwu_code'], 'fuwu_img_code' => $mycode['fuwu_img_code']], '请求成功');
    }

    /**
     * 个人中心
     */
    public function mymessage()
    {
        $userid = $this->user_id;
        $model = model('fupersonal');
        $myResult = $model->info(['id' => $userid], 'avtar,name,sex,birthday,cardstatus,phone,address,kouhao,gradeid,skillid,prevtype,money,prevcode');
        $gradename = model('fugrade')->info(['id' => $myResult['gradeid']], 'name');
        if ($gradename) {
            $myResult['gradename'] = $gradename['name'];
        } else {
            $myResult['gradename'] = '';
        }
        if ($myResult['name']  === null) {
            $myResult['name'] = '暂未认证';
        }

        if ($myResult['birthday']  === null) {
            $myResult['birthday'] = '';
        }
        if ($myResult['address']  === null) {
            $myResult['address'] = '';
        }
        if ($myResult['kouhao']  === null) {
            $myResult['kouhao'] = '';
        }
        #如果上级是企业的话，返回企业名称
        $myResult['enterprise_name'] = '';
        if ($myResult['prevtype'] == 2) {
            $member_info = db('member')->where(['invite_code' => $myResult['prevcode']])->find();
            $myResult['enterprise_name'] = $member_info['enterprise_name'];
        }
        if ($myResult['prevtype'] == 4) {
            $myResult['prevtype'] = '个人';
        } else {
            $myResult['prevtype'] = '企业';
        }
        $skillname = model('fuskill')->info(['id' => $myResult['skillid']], 'name');
        if ($skillname) {
            $myResult['skillname'] = $skillname['name'];
        } else {
            $myResult['skillname'] = '';
        }
        if ($myResult['sex'] == 0) {
            $myResult['sexname'] = '保密';
        } elseif ($myResult['sex'] == 1) {
            $myResult['sexname'] = '男';
        } elseif ($myResult['sex'] == 2) {
            $myResult['sexname'] = '女';
        }
        if ($myResult['cardstatus'] == 0) {
            $myResult['cardname'] = '暂未认证';
        } elseif ($myResult['cardstatus'] == 1) {
            $myResult['cardname'] = '认证成功';
        } elseif ($myResult['cardstatus'] == 2) {
            $myResult['cardname'] = '认证失败';
        } elseif ($myResult['cardstatus'] == 3) {
            $myResult['cardname'] = '已提交信息,等待认证';
        }
        return json_success_return($myResult, '请求成功');
    }

    /**
     * Notes:修改个人信息
     * User: shen
     * Date: 2020/5/21
     * Time: 8:42
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function updateMessage()
    {
        if (!(input('value'))) {
            return json_fail_return('请正确输入信息');
        }
        $value = input('value');
        $status = input('status') ? input('status') : 0;
        $userid = $this->user_id;
        $field = '';
        //      头像0,性别1,出生日期2,联系地址3,口号修改4
        if ($status == 0) {
            $field = 'avtar';
        } elseif ($status == 1) {
            $field = 'sex';
            if ($value == 3) {
                $value = 0;
            }
        } elseif ($status == 2) {
            $field = 'birthday';
        } elseif ($status == 3) {
            $field = 'address';
        } elseif ($status == 4) {
            $field = 'kouhao';
        }
        $model = model('fupersonal');
        $bool = $model->edit(['id' => $userid], [$field => $value]);
        if($status == 0){
            $member_info = db('member')->where(['member_id' => $this->user_id])->find();
            $file_data['file_name'] = $value;
            #极光更新头像
            updateUserAvtar($file_data,'fuwu'.$member_info['member_name']);
        }
        if ($bool !== false) {
            return json_success_return([], '修改成功');
        } else {
            return json_fail_return('修改失败');
        }
    }

    /**
     * 等级说明
     */
    public function getgrade()
    {
        $userid = $this->user_id;
        $personalmodel = model('fupersonal');
        $gradeid = $personalmodel->info(['id' => $userid], 'gradeid');
        $gradeArr = model('fugrade')->info(['id' => $gradeid['gradeid'], 'status' => 1], 'id,name,desc');
        return json_success_return($gradeArr);
    }

    /**
     * 实名认证
     */
    public function realname()
    {
        if (!(input('name')) || !(input('idcard')) || input('cardImgZ') == 'null' || input('cardImgF') == 'null'
            || input('cardImgS') == 'null' || !(input('learning')) || !(input('work'))) {
            return json_fail_return('请正确输入信息');
        }
        $userid = $this->user_id;
        $config = db('config')->where('code', 'qiniu_http')->find();;
        $data = [
            'name' => input('name'),
            'idcard' => input('idcard'),
            'cardImgZ' => input('cardImgZ'),
            'cardImgF' => input('cardImgF'),
            'cardImgS' => input('cardImgS'),
            'learning' => input('learning'),
            'work' => input('work'),
            'cardstatus' => 3
        ];
        $personalmodel = model('fupersonal');
        $bool = $personalmodel->edit(['id' => $userid], $data);
        if ($bool !== false) {
            return json_success_return([], '提交信息成功,等待认证');
        } else {
            return json_fail_return('提交信息失败');
        }
    }

    /**
     * 实名认证详情
     */
    public function realnameDetail()
    {
        $userid = $this->user_id;
        $personalmodel = model('fupersonal');
        $info = $personalmodel->info(['id' => $userid], 'name,idcard,cardImgZ,cardImgF,cardImgS,cardstatus,cardfail_resonse,learning,work');
        if ($info['cardstatus'] == 0) {
            $info['cardname'] = '未认证';
        } elseif ($info['cardstatus'] == 1) {
            $info['cardname'] = '认证成功';
        } elseif ($info['cardstatus'] == 2) {
            $info['cardname'] = '认证失败';
        } elseif ($info['cardstatus'] == 3) {
            $info['cardname'] = '已提交信息,等待认证';
        }
        return json_success_return($info);
    }

    /**
     * 意见反馈
     */
    public function mbfeedback()
    {
        if (!(input('content'))) {
            json_fail_return('请正确输入信息');
        }
        $userid = $this->user_id;
        $model = model('fupersonal');
        $myResult = $model->info(['id' => $userid], 'name,phone');
        $data = [
            'mbfb_content' => input('content'),
            'mbfb_type' => 3, //服务端
            'mbfb_time' => time(),
            'mbfb_image' => input('image') ? input('image') : '',
            'member_id' => $userid,
            'member_name' => $myResult['phone'],
        ];
        $model2 = model('mbfeedback');
        $bool = $model2->addMbfeedback($data);
        if ($bool) {
            return json_success_return([], '反馈成功');
        } else {
            return json_success_return('反馈失败');
        }
    }

    /***
     * 我的收藏列表
     */
    public function mycollect()
    {
        $userid = $this->user_id;
        $s = input('qu') ? input('qu') : 2;
        //教程(1,2),资料(1,3)
        //        2：教程 3：技术资料
        $type = input('type');
        $condition = [
            'cuc.userid' => $userid,
            'cuc.status' => 1,
            'cuc.type' => 0,
            'c.status' => 0,
            'c.is_del' => 0,
            // 'c.type'=>1,
            'cuc.type2' => $type,
        ];
        $page = input('page') ? input('page') : 1;
        $pagesize = input('pagesize') ? input('pagesize') : 15;
        $field = 'c.title,c.img,c.id,c.brand_name,c.gc_name,c.like_num';
        $res = db('course_user_collect')->alias('cuc')
            ->where($condition)->field($field)
            ->join('ds_course c', 'c.id = cuc.course_id', 'left')
            ->order('cuc.id desc')->paginate($pagesize, false, ['query' => request()->param()]);
        $list = $res->items();
        $file_http = db('config')->where('code', 'qiniu_http')->find();
        foreach ($list as $k => $v) {
            $list[$k]['img'] = $file_http['value'] . '/' . $v['img'];
            if (!$v['like_num']) {
                $ress = db('course_user_collect')
                    ->where(['course_id' => $v['id']])
                    ->field('count(*) as count')->find();
                $list[$k]['like_num'] = $ress['count'];
            }
        }
        return json_success_return($list);
    }

    /**
     * 忘记密码
     */
    public function forgetPwd()
    {
        if (!($phone = input('phone')) || !($password = input('password')) || !($password2 = input('password2')) || !($code = input('code'))) {
            return json_fail_return('请正确输入信息');
        }
        // if (!checkCode($phone,$code))
        // {
        //     return json_fail_return('验证码错误');
        // }
        if ($password != $password2) {
            return json_fail_return('两次密码输入不一致');
        }
        $condition = [
            'phone' => $phone
        ];
        $model = model('fupersonal');
        $info = $model->info($condition, 'id');
        if (empty($info)) {
            return json_fail_return('账号错误');
        }
        $bool = $model->edit($condition, ['password' => md5($password)]);
        if ($bool !== false) {
            return json_success_return([], '修改成功');
        } else {
            return json_fail_return('修改失败');
        }
    }

    /**
     * 设置工作时间
     */
    public function setTime()
    {
        if (!($week = input('week')) || !($time = input('time'))) {
            return json_fail_return('请正确输入信息');
        }
        if (!(strpos($week, '-')) || !(strpos($time, '-'))) {
            return json_fail_return('请正确输入信息');
        }
        $weekArr = explode('-', $week);
        $timeArr = explode('-', $time);
        $startweek = array_search($weekArr[0], getWeek());
        $endweek = array_search($weekArr[1], getWeek());
        $userid = $this->user_id;
        $model = model('fupersonal');
        $bool = $model->edit(
            ['id' => $userid],
            [
                'startweek' => $startweek, 'endweek' => $endweek, 'starttime' => $timeArr[0], 'endtime' => $timeArr[1]
            ]
        );
        if ($bool !== false) {
            return json_success_return([], '修改成功');
        } else {
            return json_fail_return('修改失败');
        }
    }

    /**
     * 获取工作时间
     */
    public function getTime()
    {
        $userid = $this->user_id;
        $personalmodel = model('fupersonal');
        $arr = $personalmodel->info(['id' => $userid], 'startweek,endweek,starttime,endtime');
        if ($arr['startweek']) {
            $weekArr = getWeek();
            $timestr = $weekArr[$arr['startweek']] . '-' . $weekArr[$arr['endweek']] . ' ' . $arr['starttime'] . '-' . $arr['endtime'];
        } else {
            $timestr = '';
        }
        return json_success_return([$timestr]);
    }

    //获取资讯分类
    public function getInformationClass()
    {

        $informationclass = db('informationclass')->where(['ac_parent_id' => 0])->select();

        json_success_return($informationclass);
    }

    //资讯列表
    public function getInformationList()
    {
        $data = input('post.');
        $ac_id = intval(input('ac_id'));
        $type = intval(input('type')); #0客户端 1服务端

        $where = [
            'ac_id' => $ac_id,
            'article_show' => 1,
            'ac_address' => ['in', "{$type},2"]
        ];
        if (!empty($data['information_title'])) {
            $where['article_title'] = array('like', '%' . $data['information_title'] . '%');
        }
        $page = intval(input('page')) ? intval(input('page')) : 1;
        $pagesize = intval(input('pagesize')) ? intval(input('pagesize')) : 15;
        $offset = ($page - 1) * $pagesize;

        $config = db('config')->where('code', 'qiniu_http')->find();
        $informationclass = [];
        $informationclass = db('information')
            ->where($where)
            ->order('article_thumbs', 'desc')
            ->limit($offset, $pagesize)
            ->select();
        if (!empty($informationclass)) {
            foreach ($informationclass as $key => $value) {
                $informationclass[$key]['article_pic'] = $config['value'] . '/' . $value['article_pic'];
                $informationclass[$key]['article_time'] = date('Y-m-d H:i:s', $value['article_time']);
                $article_content = htmlspecialchars_decode($value['article_content']);
                $new_content = strip_tags($article_content);
                $informationclass[$key]['article_content'] = $new_content;
                //需要判断当前用户是否点赞
                $parms = [
                    'member_id' => $this->user_id,
                    'article_id' => $value['article_id']
                ];
                $thumbs_info = db('information_thumbs')->where($parms)->find();
                $informationclass[$key]['is_thumbs'] = 1; #0--已点赞 1--未点赞 
                if (!empty($thumbs_info) && $thumbs_info['status'] == 0) {
                    $informationclass[$key]['is_thumbs'] = 0;
                }
            }
        }

        json_success_return($informationclass);
    }

    //资讯详情
    public function getInformationDetail()
    {
        $article_id = intval(input('article_id'));
        $where = [
            'article_id' => $article_id,
        ];
        $information = db('information')->where($where)->find();
        $config = db('config')->where('code', 'qiniu_http')->find();
        $information['article_pic'] = $config['value'] . '/' . $information['article_pic'];
        $information['article_time'] = date('Y-m-d H:i:s', $information['article_time']);
        $article_content = htmlspecialchars_decode($information['article_content']);
        // $new_content = strip_tags($article_content);
        $information['article_content'] = $article_content;

        #阅读量 +1
        $new_information['article_click'] = $information['article_click'] + 1;
        db('information')->where($where)->update($new_information);

        $parms = [
            'article_id' => $article_id,
            'member_id' => $this->user_id
        ];
        $thumbs_info = db('information_thumbs')->where($parms)->find();

        $information['is_thumbs'] = 1; #0--已点赞 1--未点赞 
        if (!empty($thumbs_info) && $thumbs_info['status'] == 0) {
            $information['is_thumbs'] = 0;
        }
        json_success_return($information);
    }

    //资讯点赞
    public function getInformationThumbs()
    {
        $article_id = intval(input('article_id'));
        $where = [
            'article_id' => $article_id,
        ];
        $information = db('information')->where($where)->find();

        #插入点赞数据
        #判断是否有点赞数据
        $parms = [
            'article_id' => $article_id,
            'member_id' => $this->user_id,
            'type' => 2
        ];

        $thumbs_info = db('information_thumbs')->where($parms)->find();

        if (empty($thumbs_info)) {
            db('information_thumbs')->insertGetId($parms);

            #点赞量 +1
            $new_information['article_thumbs'] = $information['article_thumbs'] + 1;
        } else {
            if ($thumbs_info['status'] == 0) {
                db('information_thumbs')->where(['id' => $thumbs_info['id']])->update(['status' => 1]);
                #点赞量 -1
                $new_information['article_thumbs'] = $information['article_thumbs'] - 1;
            } else {
                db('information_thumbs')->where(['id' => $thumbs_info['id']])->update(['status' => 0]);
                #点赞量 +1
                $new_information['article_thumbs'] = $information['article_thumbs'] + 1;
            }
        }
        db('information')->where($where)->update($new_information);
        json_success_return([], '点赞成功');
    }

    //返回体现利率
    public function serviceMoneyRadio()
    {
        $config = db('config')->where('code', 'service_money_radio')->find();
        json_success_return($config['value']);
    }
    /**
     * @name: xiaozou
     * @title: 两端获取消息推送信息
     * @url: 
     * @param {type} 
     * @return: 
     */
    public function get_affiches_order()
    {
        $parms = [
            'notif_type' => ['eq', '2'],
            'member_id' => $this->user_id,
            'member_type' => 1,
        ];
        $affiche_list = db('affiches_order')->where($parms)->select();
        foreach ($affiche_list as $k => $v) {
            $affiche_list[$k]['c_time'] = date('Y-m-d H:i:s', $v['c_time']);
        }
        json_success_return($affiche_list);
    }
    /**
     * @name: xiaozou
     * @title: 消息详情
     * @url: 
     * @param {type} 
     * @return: 
     */
    public function get_affiches_order_info()
    {
        $affiche_id = input('affiche_id', '');
        $affiche_list = db('affiches_order')->find($affiche_id);
        $affiche_list['c_time'] = date('Y-m-d H:i:s', $affiche_list['c_time']);
        db('affiches_order')->where(['id' => $affiche_id])->update(['is_read' => 1]);
        json_success_return();
    }
}
